package com.ccrfid.rmc6.resource.open;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.nightpatrol.NpGroupMemberEntity;
import com.ccrfid.rmc6.entity.business.nightpatrol.NpGroupViewEntity;
import com.ccrfid.rmc6.entity.business.nightpatrol.NpResultDetailEntity;
import com.ccrfid.rmc6.entity.business.nightpatrol.NpResultEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelBloodPressureEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelHeartRateEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelTemperatureEntity;
import com.ccrfid.rmc6.pojo.dto.open2.CardHeartRateDto;
import com.ccrfid.rmc6.pojo.dto.open2.LocatePositionDto;
import com.ccrfid.rmc6.pojo.dto.open2.LocationDto;
import com.ccrfid.rmc6.pojo.dto.open2.OpenPersonnelDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.nightpatrol.NightPatrolService;
import com.ccrfid.rmc6.service.open.OpenV2Service;
import com.ccrfid.rmc6.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Path("open/v2")
@Produces(MediaType.APPLICATION_JSON)
public class OpenV2Resource {

    @Autowired
    private OpenV2Service openV2Service;
    @Autowired
    private NightPatrolService nightPatrolService;

    //获取全部人员信息
    @GET
    @Path("personnel")
    public List<OpenPersonnelDto> getAllPersonnel() {
        return openV2Service.getAllPersonnel();
    }

    //根据人员监所唯一编号，获取某个人员信息
    @GET
    @Path("personnel/{personnelSn}")
    public OpenPersonnelDto getPersonnelBySn(@PathParam("personnelSn") String personnelSn) throws AppException {
        return openV2Service.getPersonnelBySn(personnelSn);
    }

    //根据人员内部ID编号，获取某个人员信息
    @GET
    @Path("personnel/id-{personnelId:[0-9]*}")
    public OpenPersonnelDto getPersonnelById(@PathParam("personnelId") Long personnelId) throws AppException {
        return openV2Service.getPersonnelById(personnelId);
    }

    //获取某人当前位置信息
    @GET
    @Path("personnel/{personnelSn}/locate-position")
    public LocatePositionDto getPersonnelLocatePosition(@PathParam("personnelSn") String personnelSn)
            throws AppException {
        return openV2Service.getPersonnelLocatePosition(personnelSn);
    }

    @GET
    @Path("personnel/id-{personnelId:[0-9]*}/locate-position")
    public LocatePositionDto getPersonnelLocatePositionById(@PathParam("personnelId") Long personnelId)
            throws AppException {
        return openV2Service.getPersonnelLocatePosition(personnelId);
    }

    //获取某人某段时间内的历史轨迹
    @GET
    @Path("personnel/{personnelSn}/locate-position-records")
    public List<LocatePositionDto> getPersonnelLocatePositionRecords(@PathParam("personnelSn") String personnelSn,
                                                                     @QueryParam("startTime") Timestamp startTime,
                                                                     @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getPersonnelLocatePositionRecords(personnelSn, startTime, stopTime);
    }

    //默认获取3级地图区域信息
    @GET
    @Path("locations")
    public List<LocationDto> getAllLocations() {
        return openV2Service.getAllLocationsByMapLevel(3);
    }

    //获取某个地图层级区域信息
    @GET
    @Path("locations/{mapLevel:[0-9]*}-level")
    public List<LocationDto> getAllLocationsByMapLevel(@PathParam("mapLevel") Integer mapLevel) {
        return openV2Service.getAllLocationsByMapLevel(mapLevel);
    }

    //获取某个区域内(locationId)当前人员信息(总数/清单)
    @GET
    @Path("locations/{locationId:[0-9]*}/personnel")
    public List<OpenPersonnelDto> getPersonnelInLocationById(@PathParam("locationId") Long locationId) throws AppException {
        return openV2Service.getPersonnelInLocationById(locationId);
    }

    //获取某个区域(locationSn)内当前人员信息(总数/清单)
    @GET
    @Path("locations/sn-{locationSn}/personnel")
    public List<OpenPersonnelDto> getPersonnelInLocationBySn(@PathParam("locationSn") String locationSn) throws AppException {
        return openV2Service.getPersonnelInLocationBySn(locationSn);
    }

    //获取某个区域内(locationId)关联摄像头信息
    @GET
    @Path("locations/{locationId:[0-9]*}/link-cameras")
    public List<String> getCameraIdListInLocation(@PathParam("locationId") Long locationId) {
        return openV2Service.getCameraIdListInLocation(locationId);
    }

    //获取某个标签某段时间内的计步数
    @GET
    @Path("vital-sign/card/{cardNumber}/step-count")
    public Integer getStepCountByCardNumber(@PathParam("cardNumber") Long cardNumber,
                                            @QueryParam("startTime") Timestamp startTime,
                                            @QueryParam("stopTime") Timestamp stopTime) throws AppException {
        return openV2Service.getStepCountByCardNumber(cardNumber, startTime, stopTime);
    }

    @GET
    @Path("vital-sign/personnel/{personnelId}/step-count")
    public Integer getStepCountByPersonnelId(@PathParam("personnelId") Long personnelId,
                                             @QueryParam("startTime") Timestamp startTime,
                                             @QueryParam("stopTime") Timestamp stopTime) throws AppException {
        return openV2Service.getStepCountByPersonnelId(personnelId, startTime, stopTime);
    }

    //获取某个标签某段时间内的心率记录
    @GET
    @Path("vital-sign/card/{cardNumber}/heart-rate-records")
    public List<CardHeartRateDto> getHeartRateRecordsByCardNumber(@PathParam("cardNumber") Long cardNumber,
                                                                  @QueryParam("startTime") Timestamp startTime,
                                                                  @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getHeartRateRecordsByCardNumber(cardNumber, startTime, stopTime);
    }

    @GET
    @Path("vital-sign/personnel/{personnelId}/heart-rate-records")
    public List<PersonnelHeartRateEntity> getHeartRateRecordsByPersonnelId(@PathParam("personnelId") Long personnelId,
                                                                           @QueryParam("startTime") Timestamp startTime,
                                                                           @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getHeartRateRecordsByPersonnelId(personnelId, startTime, stopTime);
    }

    //获取某个标签某段时间内的血压记录
    @GET
    @Path("vital-sign/card/{cardNumber}/blood-pressure-records")
    public List<PersonnelBloodPressureEntity> getBloodPressureRecordsByCardNumber(@PathParam("cardNumber") Long cardNumber,
                                                                                  @QueryParam("startTime") Timestamp startTime,
                                                                                  @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getBloodPressureRecordsByCardNumber(cardNumber, startTime, stopTime);
    }

    @GET
    @Path("vital-sign/personnel/{personnelId}/blood-pressure-records")
    public List<PersonnelBloodPressureEntity> getBloodPressureRecordsByPersonnelId(@PathParam("personnelId") Long personnelId,
                                                                                   @QueryParam("startTime") Timestamp startTime,
                                                                                   @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getBloodPressureRecordsByPersonnelId(personnelId, startTime, stopTime);
    }

    //获取某个标签某段时间内的体温记录
    @GET
    @Path("vital-sign/card/{cardNumber}/temperature-records")
    public List<PersonnelTemperatureEntity> getTemperatureRecordsByCardNumber(@PathParam("cardNumber") Long cardNumber,
                                                                              @QueryParam("startTime") Timestamp startTime,
                                                                              @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getTemperatureRecordsByCardNumber(cardNumber, startTime, stopTime);
    }

    @GET
    @Path("vital-sign/personnel/{personnelId}/temperature-records")
    public List<PersonnelTemperatureEntity> getTemperatureRecordsByPersonnelId(@PathParam("personnelId") Long personnelId,
                                                                               @QueryParam("startTime") Timestamp startTime,
                                                                               @QueryParam("stopTime") Timestamp stopTime)
            throws AppException {
        return openV2Service.getTemperatureRecordsByPersonnelId(personnelId, startTime, stopTime);
    }

    //查询所有的巡更组信息
    @GET
    @Path("getAllGroups")
    public Page<NpGroupViewEntity> getNpRecordPage(@QueryParam("groupName") String groupName,
                                                   @QueryParam("personnelName") String personnelName,
                                                   @QueryParam("patrolPlace") String patrolPlace,
                                                   @QueryParam("patrolTime") String patrolTime,
                                                   @BeanParam PageParam pageParam) {

        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (groupName != null) {
            criteria.add(new Criterion("groupName", Criterion.Operator.LIKE, '%' + groupName + '%'));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (patrolPlace != null) {
            criteria.add(new Criterion("patrolPlace", Criterion.Operator.LIKE, '%' + patrolPlace + '%'));
        }

        if (patrolTime != null) {
            String[] time = patrolTime.split(",");
            java.util.Date startTime = DateUtil.parseDateStr(time[0].toString());
            java.util.Date endTime = DateUtil.parseDateStr(time[1].toString());
            criteria.add(new Criterion("startTime", Criterion.Operator.LTE, startTime));
            criteria.add(new Criterion("endTime", Criterion.Operator.GTE, endTime));
        }

        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        return nightPatrolService.getNpGroupView(criteria, pageRequest);
    }

    //获取巡更组下属成员
    @GET
    @Path("getPatrolManByGroupId/{groupId}")
    public List<NpGroupMemberEntity> getAllNpgroupMembersByGroupId(@PathParam("groupId") Long groupId) {
        return nightPatrolService.getAllGroupMembersByGroupId(groupId);
    }

    //查询巡更记录
    @GET
    @Path("getRecords")
    public Page<NpResultEntity> getRcRecordPage(@QueryParam("startPatrolPlace") String startPatrolPlace,
                                                @QueryParam("patrolTime") String patrolTime,
                                                @QueryParam("groupName") String groupName,
                                                @BeanParam PageParam pageParam) {
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return nightPatrolService.getNpResultInfo(startPatrolPlace, patrolTime, groupName, pageParam, pageRequest);
    }

    //查询所有的巡更组巡更详情信息
    @GET
    @Path("getDetailByResultId/{resultId}")
    public Page<NpResultDetailEntity> getNpResultDetail(@PathParam("resultId") Long resultId,
                                                        @QueryParam("personnelName") String personnelName,
                                                        @QueryParam("type") int type,
                                                        @QueryParam("timeLength") Long timeLength,
                                                        @BeanParam PageParam pageParam) {
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return nightPatrolService.getNpResultDetailInfo(resultId, personnelName, type, timeLength, pageParam, pageRequest);
    }

}
